package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import DAO.ZCTB;

import com.et.mvc.JsonView;

/**
 * Servlet implementation class CKTB
 */
public class selectAssetTbServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public selectAssetTbServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	    request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain");
		PrintWriter out = response.getWriter();
		
        int start = Integer.parseInt(request.getParameter("start"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        
        String assetNo = request.getParameter("assetNo");
        String originalUnit = request.getParameter("originalUnit");
        String assetName = request.getParameter("assetName");
        String changeStartTime = request.getParameter("changeStartTime");
        String changeEndingTime = request.getParameter("changeEndingTime");
        
        ZCTB zctb = new ZCTB();
        
		List list = null;
        Map<String,Object> map = new HashMap<String,Object>();
        
        int total = 0;
        
        if(assetNo.equals("") && originalUnit.equals("") && assetName.equals("") && changeStartTime.equals("") && changeEndingTime.equals(""))
        {
        	 total = zctb.getTotal(); 
        	 list = zctb.getAllAssetTb(start , limit);          	        	         	
        }           
        else
        {
        	 list = zctb.getAssetTbByQuery(start, limit, assetNo, assetName, originalUnit, changeStartTime, changeEndingTime);
        	 total = zctb.getTotalByQuery();       	
        }
        
        map.put("result",list);  
        map.put("total",total);
        
        out.print(new JsonView(map));		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
